package net.xdclass.component.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.component.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @author guangyang
 * @since 2023-01-17
 **/
@Slf4j
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.from}")
    private String from;

    /**
     * 发送邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    @Override
    public void sendMail(String to, String subject, String content) {
//        创建对象
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//         发件人
        simpleMailMessage.setFrom(from);
//        收件人
        simpleMailMessage.setTo(to);
//        主题
        simpleMailMessage.setSubject(subject);
//        内容
        simpleMailMessage.setText(content);
//        发送
        javaMailSender.send(simpleMailMessage);

        log.info("邮件发送成功{}",simpleMailMessage.toString());

    }
}
